package com.example.hotel_lxy.ui.dialog;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.hotel_lxy.R;
import com.example.hotel_lxy.utils.DownloadUtils;
import com.ysxsoft.common_base.utils.DisplayUtils;


/**
* 版本
* create by Sincerly on 9999/9/9 0009
**/
public class DialPhoneDialog extends Dialog {
    private Context mContext;

    private static String mobile;
    private TextView phone;
    private TextView cancel;
    private TextView call;
    public DialPhoneDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    private View init() {
        View view = View.inflate(mContext, R.layout.dialog_dialphone, null);
        phone=view.findViewById(R.id.phone);
        cancel=view.findViewById(R.id.cancel);
        call=view.findViewById(R.id.call);
        phone.setText(""+mobile);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(mContext, Manifest.
                        permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    /**
                     * 调用ActivityCompat.requestPermissions 申请授权
                     * 参数1：上下文
                     * 参数2：要申请的权限名数组
                     * 参数3：请求码，传入唯一值 1 即可
                     */
                    ActivityCompat.requestPermissions((Activity) mContext, new String[]{Manifest.permission.CALL_PHONE} ,1);
                }else {
                    call("tel:" +mobile);

                }

            }
        });
        return view;
    }

    public void call(String telPhone){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse(telPhone));
        mContext.startActivity(intent);
        dismiss();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        setContentView(init());
    }

    public void showDialog() {
        if (!isShowing()) {
            show();
            WindowManager.LayoutParams lp = getWindow().getAttributes();
//            lp.height = DisplayUtils.getDisplayWidth(mContext) * 5 / 5;
            lp.width = DisplayUtils.getDisplayWidth(mContext) * 5 / 10;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            getWindow().setAttributes(lp);
            getWindow().setGravity(Gravity.CENTER);
        }
    }

    public static DialPhoneDialog show(Context context, OnDialogClickListener listener,String mobiles) {
        DialPhoneDialog dialog = new DialPhoneDialog(context, R.style.BottomDialogStyle);
        dialog.setListener(listener);
        mobile=mobiles;
        dialog.showDialog();
        return dialog;
    }

    public OnDialogClickListener listener;

    public DialPhoneDialog setListener(OnDialogClickListener listener) {
        this.listener = listener;
        return this;
    }
    public interface OnDialogClickListener {
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick(String s);
    }
}